package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import classes.Korisnik;

public class KorisnikDAO {
	@SuppressWarnings("unused")
	private final Connection c;
	private final PreparedStatement qInsert;
	private final PreparedStatement qDelete;
	private final PreparedStatement qSelectSingleFullMatch;
	private final PreparedStatement qSelectAll;
	private final PreparedStatement qUpdate;
	private final PreparedStatement qSelectById;

	public KorisnikDAO(Connection c) throws Exception {
		this.c = c;
		qInsert=c.prepareStatement("INSERT INTO korisnik( ime, prezime, adresa, telefon, username, password) VALUES (?,?,?,?,?,?)");
		qDelete = c.prepareStatement("DELETE FROM korisnik WHERE idKorisnik=?");
		qSelectSingleFullMatch = c.prepareStatement("SELECT * FROM korisnik WHERE username=? LIMIT 1");
		qSelectById = c.prepareStatement("SELECT * FROM korisnik WHERE idKorisnik=? ");

		qSelectAll = c.prepareStatement("SELECT * FROM korisnik");
		qUpdate=c.prepareStatement("UPDATE  korisnik SET idKorisnik=?, ime=?, prezime=?, adresa=?, telefon=?, username=?, password=?");
		
	}
	
	public int insertKorisnik(Korisnik k) throws Exception {
		try {
			qInsert.setString(1, k.getIme());
			qInsert.setString(2, k.getPrezime());
			qInsert.setString(3, k.getAdresa());
			qInsert.setString(4, k.getTelefon());
			qInsert.setString(5, k.getUsername());
			qInsert.setString(6, k.getPassword());
			int a=qInsert.executeUpdate();
			return a; 
		} catch(Exception e) {
			e.printStackTrace(); 
			return 0;
		} 
	}
	
	public int updateKorisnik(Korisnik k) throws Exception {
		try{
		if (k==null)
			throw new Exception("Nije moguce mijenjati nepostojeceg korisnika!");
		
		qSelectSingleFullMatch.setString(1, k.getUsername());
		ResultSet rs = qSelectSingleFullMatch.executeQuery();
		rs.next();
		qUpdate.setInt(1, rs.getInt(1)+1);
		qUpdate.setString(2, k.getIme());
		qUpdate.setString(3, k.getPrezime());
		qUpdate.setString(4, k.getAdresa());
		qUpdate.setString(5, k.getTelefon());
		qUpdate.setString(6, k.getUsername());
		qUpdate.setString(7, k.getPassword());

		return qUpdate.executeUpdate();
		
		} catch(Exception e){
			e.printStackTrace();
			return 0;
		}
	}
		
	//izbrisi korisnika sa datim id
	public int deleteKorisnik(int id) throws Exception {
		qDelete.setInt(1, id);
		return qDelete.executeUpdate();
	}
	
	//select korisnika sa datim punim username-om
	public Korisnik selectKorisnik(String i) throws Exception {
		qSelectSingleFullMatch.setString(1, i);
		ResultSet rs = qSelectSingleFullMatch.executeQuery();
		if (rs.next()==false)
			throw new Exception("Nema takvog korisnika");
		return ucitajKorisnikaIzResultSeta(rs);
	}
	//select korisnika sa datim id 
		public Korisnik selectKorisnikById(int i) throws Exception {
			qSelectSingleFullMatch.setInt(1, i);
			ResultSet rs = qSelectById.executeQuery();
			if (rs.next()==false)
				throw new Exception("Nema takvog korisnika");
			return ucitajKorisnikaIzResultSeta(rs);
		}
		
		
	//select svi korisnici
	public List<Korisnik> selectSviKorisnici() throws Exception{
		ResultSet rs = qSelectAll.executeQuery();
		List<Korisnik> korisnici = new ArrayList<Korisnik>();
		while (rs.next()){
			korisnici.add(ucitajKorisnikaIzResultSeta(rs));
		}
		return korisnici;
	}
	
		//pomocna funkcija
	private Korisnik ucitajKorisnikaIzResultSeta(ResultSet rs) throws Exception{
		Korisnik k = new Korisnik();
		k.setId(rs.getInt(1));
		k.setIme(rs.getString(2));
		k.setPrezime(rs.getString(3));
		k.setAdresa(rs.getString(4));
		k.setTelefon(rs.getString(5));
		k.setUsername(rs.getString(6));
		k.setPassword(rs.getString(7));
		return k;
	}
	
}
